package cn.jsj.onclock.service.impl;

import cn.jsj.onclock.dao.AdDao;
import cn.jsj.onclock.pojo.AdEnt;
import cn.jsj.onclock.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 卢斌
 */
@Service
public class AdServiceImpl implements AdService {

    private final AdDao adDao;

    @Autowired
    public AdServiceImpl(AdDao adDao) {
        this.adDao = adDao;
    }

    /**
     * 随机查询一个广告信息
     *
     * @return 获取一个随机广告
     */
    @Override
    public AdEnt getRandomAd() {
        Long total = adDao.countAdTotalNumber();
        if (total == 0) {
            return null;
        }
        return adDao.getAdEntById(( long) (Math.random() * total) + 1);
    }

}
